// src/composables/useWatchText.ts
import { ref, watch, Ref } from "vue";

export function useWatchText(props: any, emit: any, textFn?, emitFn?): Ref<any> {
	const dataValue = ref(props.modelValue);

	watch(
		() => props.modelValue,
		(data) => {
			if (textFn) {
				dataValue.value = textFn(data);
			} else {
				dataValue.value = data;
			}
			emit("update:modelValue", dataValue.value);
			emit("update:fieldShow", dataValue.value);
		},
		{ immediate: true }
	);

	watch(
		() => dataValue.value,
		(data) => {
			let info = data;
			if (emitFn) {
				info = emitFn(data);
			}
			emit("update:modelValue", info);
			emit("update:fieldShow", info);
		}
	);

	return dataValue;
}
